//
//  LGOutpatientRecordCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/9.
//

#import "LGOutpatientRecordPicCell.h"

@interface LGOutpatientRecordPicCell()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *typeButton;
/**   */
@property (nonatomic , strong)UIButton *dateButton;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)UIButton *countButton;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)UIButton *deleteBtn;
/**   */
@property (nonatomic , strong)NSArray *photoArray;
/**   */
@property (nonatomic , assign)CGFloat photoW;
@end

@implementation LGOutpatientRecordPicCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSInteger count = isPad?8:4;
        self.photoW = (CGFloat)(Screen_W-viewPix(64)-viewPix(8)*(count-1))/count;
        [self createSubView];
    }
    return  self;
}

- (void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    [self.typeButton setTitle:LGNSString(dataDic[@"visit_record_type_str"]) forState:UIControlStateNormal];
    self.typeButton.selected = [dataDic[@"visit_record_type_str"] isEqualToString:@"住院记录"]?YES:NO;
    [self.dateButton setTitle:LGNSString(dataDic[@"datestr"]) forState:UIControlStateNormal];
    [self.dateButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:0];
    self.photoArray = dataDic[@"images"];
    
    NSString *type = LGNSString(dataDic[@"type"]);
    self.deleteBtn.hidden = [type isEqualToString:@"doctor"]?NO:YES;
    CGFloat bottom = [type isEqualToString:@"doctor"]?viewPix(40):0;
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(viewPix(100)+self.photoW+bottom));
    }];
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    
    for (UIImageView *imageView in self.photoView.subviews) {
        [imageView removeFromSuperview];
    }
    
    NSInteger maxCount = isPad?8:4;
    NSInteger count = (photoArray.count>maxCount)?maxCount:photoArray.count;
    for (NSInteger i=0; i<count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.photoW+viewPix(8))*i, 0, self.photoW, self.photoW)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:photoArray[i]]];
        imageView.userInteractionEnabled = YES;
        imageView.tag = 1000+i;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkBigPhotoAction:)]];
        [self.photoView addSubview:imageView];
    }
    [self.photoView addSubview:self.countButton];
    self.countButton.hidden = (photoArray.count>maxCount)?NO:YES;
    [self.countButton setTitle:[NSString stringWithFormat:@"+%ld",photoArray.count] forState:UIControlStateNormal];
    self.countButton.frame = CGRectMake((self.photoW+viewPix(8))*(maxCount-1), 0, self.photoW, self.photoW);
    [self.photoView bringSubviewToFront:self.countButton];
    self.numberLabel.text = [NSString stringWithFormat:@"共 %ld 张图片",photoArray.count];
}

//删除报告
-(void)deleteButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(deleteReportAction:)]){
        [self.delegate deleteReportAction:self.dataDic];
    }
}

-(void)countButtonAction{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect rect = [self.countButton convertRect:self.frame toView:window];
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkPhotoWithPoint:index:photoArray:)]){
        [self.delegate checkPhotoWithPoint:CGPointMake(rect.origin.x, rect.origin.y) index:0 photoArray:self.photoArray];
    }
}


-(void)checkBigPhotoAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView = (UIImageView *)sender.view;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect rect = [sender.view convertRect:self.frame toView:window];
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkPhotoWithPoint:index:photoArray:)]){
        [self.delegate checkPhotoWithPoint:CGPointMake(rect.origin.x, rect.origin.y) index:imageView.tag-1000 photoArray:self.photoArray];
    }
}

#pragma mark
#pragma mark ====> 创建控件

-(void)createSubView{
    self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.typeButton];
    [self.baseView addSubview:self.dateButton];
    [self.baseView addSubview:self.photoView];
    [self.baseView addSubview:self.numberLabel];
    [self.photoView addSubview:self.countButton];
    [self.baseView addSubview:self.deleteBtn];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(-viewPix(8));
        make.height.equalTo(@(viewPix(100)+self.photoW));
    }];
    [self.dateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(16));
        make.centerY.equalTo(self.typeButton);
        make.width.equalTo(@(viewPix(200)));
        make.height.equalTo(@(viewPix(25)));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:8];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(8), Screen_W-viewPix(32), viewPix(175));
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}


-(UIButton *)typeButton{
    if(!_typeButton){
        _typeButton = [UIButton buttonWithTitle:@"门诊记录" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#1F6CC6" selectColor:@"#189090"];
        [_typeButton setBackgroundImage:[UIImage imageNamed:@"就诊检查-蓝色"] forState:UIControlStateNormal];
        [_typeButton setBackgroundImage:[UIImage imageNamed:@"就诊检查-绿色"] forState:UIControlStateSelected];
        _typeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _typeButton.frame = CGRectMake(0, viewPix(16), viewPix(102), viewPix(27));
        _typeButton.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(16), 0, 0);
        _typeButton.userInteractionEnabled = NO;
    }
    return _typeButton;
}


-(UIButton *)dateButton{
    if(!_dateButton){
        _dateButton = [UIButton buttonWithTitle:@"2021年12月21日"  titleFont:LGFont(16) textColor:@"#666666" imageName:@"grayTip16"];
        _dateButton.bounds = CGRectMake(0, 0, viewPix(200), viewPix(25));
        _dateButton.userInteractionEnabled = NO;
        _dateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_dateButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:0];
    }
    return _dateButton;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(59), Screen_W-viewPix(64), self.photoW)];
    }
    return _photoView;
}

-(UIButton *)countButton{
    if(!_countButton){
        _countButton = [UIButton buttonWithTitle:@"+3" titleFont:LGFontWeight(22, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _countButton.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        _countButton.cornerRidus = 3;
        _countButton.hidden = YES;
        [_countButton addTarget:self action:@selector(countButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _countButton;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"共 6 张图片" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _numberLabel.frame = CGRectMake(viewPix(16), kMaxY(self.photoView.frame)+viewPix(10), viewPix(100), viewPix(20));
    }
    return _numberLabel;
}

-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithTitle:@"删除" titleFont:LGFont(16) normalColor:@"#DD3030" selectColor:@"#DD3030"];
        _deleteBtn.frame = CGRectMake(viewPix(16), kMaxY(self.numberLabel.frame)+viewPix(10), viewPix(85), viewPix(30));
        _deleteBtn.layer.borderColor = [UIColor colorWithString:@"#DD3030"].CGColor;
        _deleteBtn.layer.borderWidth = 1;
        _deleteBtn.cornerRidus = viewPix(15);
        _deleteBtn.hidden = YES;
        [_deleteBtn addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
